<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta http-equiv="X-UA-Compatible" content="IE=edge" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>Document</title>
  </head>
  <body>
    <script>
      let arr = [1, 2, 3, 4];
      
      function createProxy(arr) {
        let option = {
          //get截取器
          get: function (target, key, receive) {
            console.log(receive);
            //重写key
            key = Number(key);
            //如果传入的key值小于0
            if (key < 0) {
              key += target.length;
            }
            //正常情况直接返回调用
            return target[key];
          },
        };
        //给数组创建一个代理
        return new Proxy(arr, option);
      }
      arr = createProxy(arr);
      console.log(arr[-1]); //需要使结果输出为4
    </script>
  </body>
</html>
